package de.jenseins.utils.propertytranslator;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.PropertyResourceBundle;

import javax.swing.JOptionPane;

import org.apache.commons.io.FilenameUtils;

import com.google.api.translate.Language;

import de.jenseins.utils.propertytranslator.gui.FilePicker;
import de.jenseins.utils.propertytranslator.gui.IHaveProgressBar;
import de.jenseins.utils.propertytranslator.translation.Translator;

/**
 * 
 * 
 * @author Jens Greive
 * @since 01.12.2009
 * 
 */
public class PropertyTranslator {

    private final IHaveProgressBar component;

    public PropertyTranslator(final IHaveProgressBar component) {
	this.component = component;
    }

    public void translate(File sourceFile, Language sourceLang, Language targetLang) {
	PropertyResourceBundle propertyBundle = initPropertyBundle(sourceFile);
	Properties translatedProperties = translateBundle(sourceLang, targetLang, propertyBundle);
	saveTranslation(sourceFile, targetLang, translatedProperties);

    }

    private void saveTranslation(File sourceFile, Language targetLang, Properties translatedProperties) {
	File targetFile = getTargetFile(sourceFile, targetLang);
	if (targetFile != null) {
	    try {
		translatedProperties.store(new FileOutputStream(targetFile), "Automatically translated property file.");
	    } catch (FileNotFoundException e1) {
		JOptionPane.showMessageDialog(null, e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		e1.printStackTrace();
	    } catch (IOException e1) {
		JOptionPane.showMessageDialog(null, e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		e1.printStackTrace();
	    }
	}
    }

    private Properties translateBundle(Language sourceLang, Language targetLang, PropertyResourceBundle propertyBundle) {
	Properties translatedProperties = new Properties();
	for (Enumeration<String> e = propertyBundle.getKeys(); e.hasMoreElements();) {
	    String key = (String) e.nextElement();
	    String toTranslate = (String) propertyBundle.getObject(key);
	    try {
		translatedProperties.put(key, Translator.getTranslation(toTranslate, sourceLang, targetLang));
	    } catch (Exception e1) {
		JOptionPane.showMessageDialog(null, e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		e1.printStackTrace();
	    }
	    component.increasePbarValue();
	}
	return translatedProperties;
    }

    private PropertyResourceBundle initPropertyBundle(File sourceFile) {
	PropertyResourceBundle propertyBundle = null;
	try {
	    propertyBundle = new PropertyResourceBundle(new FileInputStream(sourceFile));
	} catch (FileNotFoundException e1) {
	    JOptionPane.showMessageDialog(null, e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
	    e1.printStackTrace();
	} catch (IOException e1) {
	    JOptionPane.showMessageDialog(null, e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
	    e1.printStackTrace();
	}
	return propertyBundle;
    }

    private File getTargetFile(final File sourceFile, final Language lang) {
	String dir = sourceFile.getParent();
	String suffix = FilenameUtils.getExtension(sourceFile.getName());
	String prefix = FilenameUtils.getBaseName(sourceFile.getName());
	String fileName = null;
	if (prefix.indexOf("_") != -1) {
	    String[] splittedFileName = prefix.split("_");
	    fileName = splittedFileName[0] + "_" + lang + "." + suffix;
	} else {
	    fileName = prefix + "_" + lang + "." + suffix;
	}
	File suggestedFile = new File(dir, fileName);
	File result = FilePicker.getTargetDir(suggestedFile);
	return result;
    }

}
